package com.yuyou.fn.activity.mapper;

import com.yuyou.fn.activity.entity.ActivityRegSituation;
import com.github.wens.mybatisplus.mapper.AutoMapper;
import com.yuyou.fn.activity.vo.ActivityNumVo;
import com.yuyou.fn.activity.vo.ActivitySessionNumVo;
import com.yuyou.fn.activity.vo.ActivityQuantityVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 *
 * ActivityRegSituation 表数据库控制层接口
 *
 */
public interface ActivityRegSituationMapper extends AutoMapper<ActivityRegSituation,Long> {

    @Select("<script>" +
            "<![CDATA[" +
            "SELECT activity_id AS activityId,campus_id AS campusId,COUNT(DISTINCT(CONCAT(student_name,student_phone_no)))  as allocatedQuantity from activity_reg_situation " +
            "where  activity_id in " +
            "]]>" +
            "<foreach item='item' index='index' collection='activityIds' open='(' separator=',' close=')'>\n" +
            "#{item}" +
            "</foreach>" +
            "group by activity_id,campus_id" +
            "</script>")
    @ResultType(ActivityQuantityVo.class)
    List<ActivityQuantityVo> findActivityQuantityByActivityIds(@Param("activityIds") List<Long> activityIds);

    @Select("\n" +
            "SELECT COUNT(1) AS signNum," +
            "IFNULL(SUM(CASE  when read_status=0 then 1 else 0 END) ,0) AS unReadNum ," +
            "IFNULL(SUM(CASE  when diagnosis_status=1 then 1 else 0 END) ,0) AS diagnosisNum " +
            " from activity_reg_situation where activity_campus_session_id= #{activityCampusSessionId} and sign_status=1 ")
    @ResultType(ActivitySessionNumVo.class)
    ActivitySessionNumVo findActivitySessionNum(@Param("activityCampusSessionId") long activityCampusSessionId);

    @Select("SELECT SUM(studentNum) AS studentNum,SUM(signNum) AS signNum , SUM(CASE  WHEN read_status=0 THEN studentNum ELSE 0 end) AS unReadStudentNum FROM (" +
            "SELECT COUNT(DISTINCT(CONCAT(student_name,\"_\",student_phone_no))) AS studentNum,COUNT(1) AS signNum,read_status FROM activity_reg_situation  WHERE activity_campus_id=#{activityCampusId} and sign_status=1  GROUP BY read_status) a")
    ActivityNumVo findActivityNum(@Param("activityCampusId") long activityCampusId);

}